package com.shanghaionstar.activity.webview.handler;

import android.content.Context;
import android.os.Message;

import com.onstar.cn.ShareEntity;
import com.onstar.cn.common.jsBridge.BridgeHandler;
import com.onstar.cn.common.jsBridge.BridgeWebView;
import com.onstar.cn.common.jsBridge.CallBackFunction;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.common.util.EncryptUtil;

import com.onstar.cn.framework.utils.GsonHelper;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.DevelopServerEnvironmentUtil;
import com.shanghaionstar.utils.H5JSObject;
import com.shanghaionstar.utils.JoinMeConstants;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class ForgotPassWebHandler extends DefaultWebHandler {

    public ForgotPassWebHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what) {
            case IWebType.TYPE_FORGOT_PASS:
                initForgotPassView(msg);
                break;
        }
    }

    private void initForgotPassView(Message msg) {
        String url = getResetPasswordUrl();
        if (context instanceof CommonWebViewActivity) {
            final CommonWebViewActivity mActivity = (CommonWebViewActivity) context;
            BridgeWebView webView = mActivity.getWebview();
            webView.setupDefaultSettings(webView,context);
            webView.loadUrl(url);
            webView.registerHandler("close", new BridgeHandler() {
                @Override
                public void handler(String data, CallBackFunction function) {
                    mActivity.finish();
                }
            });
            webView.registerHandler("callPhone", new BridgeHandler() {
                @Override
                public void handler(String data, CallBackFunction function) {
                    if (!CommonUtil.isNull(data)) {
                        ShareEntity shareEntity = GsonHelper.fromJson( data,ShareEntity.class);
                        if (!CommonUtil.isNull(shareEntity)) {
                            H5JSObject h5 = new H5JSObject(context);
                            h5.dial(shareEntity.getPhone());
                        }
                    }
                }
            });
        }
    }

    private String getResetPasswordUrl() {
        if (!DeviceUtil.isChineseLanguage()) {
            JoinMeConstants.reset_password_url = JoinMeConstants.reset_password_url + "&local=en";
        } else {
            JoinMeConstants.reset_password_url = JoinMeConstants.reset_password_url + "&local=cn";
        }
        return JoinMeConstants.reset_password_url;
    }




}
